Thread: String conversion to floating point[Digit grouping]

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    19

    Question String conversion to floating point[Digit grouping]

    Im currently facing an issue and can't find the information about this, the problem is my applications needs to be locale independent.
    When accessing a database i get a string representing a number for e.g:

    "123.456.789,30"

    now the dot is the digit grouping sign for my current locale, and as i found from
    several google searchs, atof doesn't consider the current locale digit group symbol.

    Now to the question is there any standard C/C++ function which does consider the digit grouping symbol in conversion from a string to a fp value ?

    Thks very much for any help/feedback

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not aware of any function that achieves that.

    It doesn't seem very hard, however, to write a little function that takes an input string, and outputs the same content minus the dots and replacing comma with a decimal point (dot).

    It'd prefer to use strtod() rather than atof(), as strtod handles errors a bit easier.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    The locale functionality in the standard library will do it.

    Code:
    #include <locale>
    #include <iostream>
    #include <sstream>
    
    int main()
    {
        std::istringstream funkynumber("123.456.789,30");
        double asDouble = 0.0;
        std::locale loc("Dutch"); // first locale I found with dots as thousands separator
        funkynumber.imbue(loc);
        funkynumber >> asDouble;
        std::cout << "As string: " << str.str() << '\n';
        std::cout << "As double: " << std::fixed << asDouble << std::endl;
    }

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    19
    Quote Originally Posted by adeyblue View Post
    The locale functionality in the standard library will do it.

    Code:
    #include <locale>
    #include <iostream>
    #include <sstream>
    
    int main()
    {
        std::istringstream funkynumber("123.456.789,30");
        double asDouble = 0.0;
        std::locale loc("Dutch"); // first locale I found with dots as thousands separator
        funkynumber.imbue(loc);
        funkynumber >> asDouble;
        std::cout << "As string: " << str.str() << '\n';
        std::cout << "As double: " << std::fixed << asDouble << std::endl;
    }
    Thanks adeyblue, i didin't wan't to reinvent the wheel :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. exact conversion of floating point to string
    By szeliat in forum C Programming
    Replies: 6
    Last Post: 07-27-2005, 09:43 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. string object to null-terminating string conversion
    By sayz0 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2001, 12:15 PM

Tags for this Thread